#include "Anim.h"

Anim::Anim(std::string true_name,std::string nom,unsigned int nbr,MainLoader *mdat,std::vector<int> V, bool reset,bool idle)
{
    abb_name=true_name;
    name=nom;
    Nb_sprite=nbr;
    seq_time=V;

    std::string temp=nom+".png";
    m_Image=mdat->Load(temp);
    sf::Color C (255,255,255);
    m_Image.CreateMaskFromColor(C);
    m_Sprite.SetImage(m_Image);


    std::cout<<std::endl<<"Anim Load : "<<nom;
    reset_seq=reset;
    idle_ret=idle;
    current_seq=0;
    current_time=0;
    droite=true;
    /*if(Nb_sprite!=(seq_sprite.size())){
        std::cout<<std::endl<<"Erreur de taille de seq_sprite";
    }*/
    if(Nb_sprite!=(seq_time.size())){
        std::cout<<std::endl<<"Erreur de taille de seq_time";
    }
}

void Anim::Pars(){

    int L=m_Sprite.GetSize().x;
    int H=m_Sprite.GetSize().y;
    int L_subrect=L/Nb_sprite;
    sf::IntRect S ((current_seq*L_subrect),0, ((current_seq+1)*L_subrect), H);
    m_Sprite.SetSubRect(S);
}

Anim::~Anim()
{
    //dtor
}

bool Anim::Ret_Idle(){
    return ((idle_ret)&&(current_seq==(Nb_sprite-1)));
}

sf::Sprite Anim::GetSprite(){
    this->Pars();
    current_time++;
    if (current_time==(seq_time[current_seq])){
        current_seq++;
        if(current_seq==Nb_sprite){
            current_seq=0;
        }
        current_time=0;
    }
    if (droite){
        return m_Sprite;
        }
        else{
            sf::Sprite inv =m_Sprite;
            inv.FlipX(true);
            return inv;
            }

}
